<template>
    <el-dialog :title="!dataForm.id ? '新增' : '修改'"
               :close-on-click-modal="false"
               width="500px"
               :visible.sync="visible">
        <el-form :model="dataForm"
                 :rules="dataRule"
                 ref="dataForm"
                 @keyup.enter.native="dataFormSubmit()"
                 label-width="110px">
            <el-form-item label="员工代码" prop="empNo">
                <el-input v-model="dataForm.empNo"></el-input>
            </el-form-item>
            <el-form-item label="员工姓名" prop="empName">
                <el-input v-model="dataForm.empName"></el-input>
            </el-form-item>
            <el-form-item label="所属部门" prop="deptId">
                <el-select v-model="dataForm.deptId" placeholder="请选择">
                    <el-option
                        v-for="item in departList"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="关联CRM账号" prop="biz1">
                <el-select v-model="dataForm.biz1" placeholder="请选择">
                    <el-option
                        v-for="item in accList"
                        :key="item.username"
                        :label="item.username"
                        :value="item.username">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="关联ERP账号" prop="biz5">
              <el-input v-model="dataForm.biz5"></el-input>
            </el-form-item>

        </el-form>
        <span slot="footer"
              class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary"
                 @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
import { Debounce } from '@/utils/debounce'
import {custTypeList,departList} from '@/utils/data'
export default {
	data () {
		return {
			visible: false,
            custTypeList: custTypeList,
            departList: departList,
            accList: [],
            provinceList: [],
            cityList: [],
            areaList: [],
			roleList: [],
            personList: [],
			dataForm: {
                empNo: "",
                empName: "",
                deptId: "",
                biz1: "",
                id: ""
			},
			dataRule: {
                empNo: [
                    { required: true, message: '员工代码不能为空', trigger: 'blur' }
                ],
                empName: [
                    { required: true, message: '员工姓名不能为空', trigger: 'blur' }
                ],
                deptId: [
                    { required: true, message: '所属部门不能为空', trigger: 'change' }
                ],
                // biz1: [
                //     { required: true, message: '不能为空', trigger: 'blur' }
                // ],
			}
		}
	},
	methods: {
		init (id) {
            console.log('🚀 ~ id', id)
			this.dataForm.id = id || 0
			this.visible = true
            console.log('🚀 ~ this.dataForm', this.dataForm)
            this.$http({
                url: this.$http.adornUrl('/sys/user/page'),
                method: 'get',
                params: this.$http.adornParams(
                    Object.assign(
                        {
                            current: 1,
                            size: 1000
                        }
                    )
                )
            }).then(({ data }) => {
                this.accList = data.records

            })
            if(this.dataForm.id){
                this.$nextTick(() => {
                    this.$refs['dataForm'].resetFields()
                    if (this.dataForm.id) {
                        this.$http({
                            url: this.$http.adornUrl('/crm/base/emp/info/' + this.dataForm.id),
                            method: 'get',
                            params: this.$http.adornParams()
                        }).then(({ data }) => {
                            this.dataForm = data
                        })
                    }
                })
            }else{
                this.dataForm = {
                    empNo: "",
                    empName: "",
                    deptId: "",
                    biz1: "",
                    id: ""
                }
            }
		},

		// 表单提交
		dataFormSubmit: Debounce(function () {
			this.$refs['dataForm'].validate((valid) => {
				if (valid) {
					this.$http({
						url: this.$http.adornUrl( this.dataForm.id ? '/crm/base/emp/saveEmp' : '/crm/base/emp/saveEmp'),
						method: 'post',
						data: this.$http.adornData({
                            ...this.dataForm
                        })
					}).then(({ data }) => {
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
								this.visible = false
								this.$emit('refreshDataList')
							}
						})
					})
				}
			})
		})
	}
}
</script>
